<template>
	<view class="order-pages">
		<view class="fix" style="top:-100%;left:-100%">
			<min-countdown :targetTime="item.start_service_time_unix * 1000" :isPlay="true"
				v-for="(item,index) in unix_list" :key="index">
			</min-countdown>
		</view>
		<fixed>
			<tab @change="handerTabChange" :list="tabList" :activeIndex="activeIndex*1" :activeColor="primaryColor"
				:width="100/tabList.length + '%'" height="100rpx"></tab>
			<view class="b-1px-b"></view>
		</fixed>
		<view @tap.stop="goDetail(index)" class="item-child mt-md ml-lg mr-lg pd-lg fill-base radius-16"
			v-for="(item,index) in list.data" :key="index">
			<view @tap.stop="goShop(index)" class="flex-between pb-lg">
				<view class="f-paragraph c-title max-500 ellipsis">订单号：{{item.order_code}}</view>
				<view class="f-caption text-bold"
					:style="{color:item.pay_type==2?primaryColor: [3,4,5].includes(item.pay_type)?subColor: item.pay_type == 6 ? '#11C95E' : '#333'}">
					{{statusType[item.pay_type]}}
				</view>
			</view>
			<view class="flex-center mb-lg" v-for="(aitem,aindex) in item.order_goods" :key="aindex">
				<image mode="aspectFill" class="avatar lg radius-16" :src="aitem.goods_cover"></image>
				<view class="flex-1 ml-md">
					<view class="flex-between">
						<view class="goods-title f-title c-title ellipsis" :class="[{'max-300':aitem.refund_num>0}]">
							{{aitem.goods_name}}
						</view>
						<view class="f-caption c-warning" v-if="aitem.refund_num>0">已退x{{aitem.refund_num}}</view>
					</view>
					<view class="f-caption c-caption mt-md">服务时间：{{item.start_time}}</view>
					<view class="flex-between">
						<view class="flex-y-baseline f-caption c-warning">¥<view class="f-title text-bold">
								{{aitem.price}}
							</view>
						</view>
						<view class="c-paragraph">x{{aitem.num}}</view>
					</view>
				</view>
			</view>
			<view class="flex-between pt-lg b-1px-t">
				<view class="flex-y-center f-desc c-title">总计：
					<view class="f-paragraph c-warning text-bold">¥{{item.pay_price}}</view>
				</view>
				<view class="flex-warp">
					<!-- // pay_type 2待接单，5待服务，6服务中，7服务完成 -->
					<!-- 待接单 -->
					<block v-if="item.pay_type == 2">
						<button @tap.stop="toRefuse(index)" class="clear-btn order">拒绝接单</button>
						<button @tap.stop="toConfirm(index,3)" class="clear-btn order"
							:style="{color:'#fff',background:primaryColor,borderColor:primaryColor}">确认接单</button>
					</block>
					<!-- 待服务 -->
					<block v-if="item.pay_type == 3 || item.pay_type == 4 || item.pay_type == 5">
						<button @tap.stop="toTel(index)" class="clear-btn order">咨询</button>
						<button @tap.stop="toConfirm(index, item.pay_type*1+1 )" class="clear-btn order"
							:style="{color:'#fff',background:primaryColor,borderColor:primaryColor}">{{item.pay_type == 3 ? '已出发': item.pay_type == 4 ?'拍照确认到达' : '开始服务'}}</button>
					</block>
					<!-- 服务中 -->
					<block v-if="item.pay_type == 6">
						<button @tap.stop="toConfirm(index,7)" class="clear-btn order"
							:style="{color:'#fff',background:primaryColor,borderColor:primaryColor}">拍照完成服务</button>
					</block>
				</view>
			</view>
		</view>

		<load-more :noMore="list.current_page>=list.last_page&&list.data.length>0" :loading="loading" v-if="loading">
		</load-more>
		<abnor v-if="!loading&&list.data.length<=0&&list.current_page==1"></abnor>

		<view class="space-footer"></view>

		<uni-popup ref="refuse_item" type="center" :custom="true">
			<view class="common-popup-content fill-base pd-lg radius-34">
				<view class="title">拒绝接单</view>
				<view class="desc">请确认是否拒接接单</view>
				<textarea v-model="coach_refund_text" class="pd-lg textarea f-desc c-title mt-lg radius-20"
					maxlength="200" placeholder="请输入拒单原因" placeholder-class="f-desc c-caption"></textarea>
				<view class="flex-center mt-md" style="width: 540rpx">
					<view class="flex-1"></view>
					<view>{{coach_refund_text.length>200?200:coach_refund_text.length}}/200</view>
				</view>
				<view class="button">
					<view @tap.stop="$refs.refuse_item.close()" class="item-child">取消</view>
					<view @tap.stop="confirmRefuse" class="item-child c-base"
						:style="{background: primaryColor,color:'#fff'}">确定</view>
				</view>
			</view>
		</uni-popup>

	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	export default {
		components: {},
		data() {
			return {
				options: {},
				activeIndex: 0,
				tabList: [{
					// 	title: '全部',
					// 	id: 0
					// }, {
					title: '待接单',
					id: 2,
					number: 0
				}, {
					title: '待服务',
					id: 5
				}, {
					title: '服务中',
					id: 6
				}, {
					title: '已完成',
					id: 7
				}],
				statusType: {
					2: '待接单',
					3: '已接单',
					4: '已出发',
					5: '已到达',
					6: '服务中',
					7: '已完成',
				},
				param: {
					page: 1,
					pay_type: 0,
				},
				list: {
					data: []
				},
				loading: true,
				index: -1,
				coach_refund_text: '',
				lockTap: false,
				unix_list: []
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			configInfo: state => state.config.configInfo,
			userInfo: state => state.user.userInfo,
		}),
		onLoad(options) {
			this.options = options
			let {
				tab = 0
			} = options
			this.activeIndex = tab
			this.initIndex()
		},
		onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			this.initRefresh();
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.list.current_page >= this.list.last_page || this.loading) return;
			this.param.page = this.param.page + 1;
			this.loading = true;
			this.getList();
		},
		methods: {
			...mapActions(['getConfigInfo']),
			...mapMutations(['']),
			async initIndex(refresh = false) {
				// #ifdef H5
				if (!refresh && this.$jweixin.isWechat()) {
					await this.$jweixin.initJssdk();
					this.$jweixin.wxReady(() => {
						this.$jweixin.hideOptionMenu()
					})
				}
				// #endif
				if (!this.configInfo.id || refresh) {
					await this.getConfigInfo()
				}
				await this.getList()
				this.$util.setNavigationBarColor({
					bg: this.primaryColor
				})
			},
			initRefresh() {
				this.param.page = 1
				this.initIndex(true)
			},
			async getList() {
				let {
					list: oldList,
					param,
					tabList,
					activeIndex
				} = this
				param.pay_type = tabList[activeIndex].id
				let newList = await this.$api.technician.orderList(param)
				if (this.param.page == 1) {
					this.list = newList
				} else {
					newList.data = oldList.data.concat(newList.data)
					this.list = newList
				}
				this.tabList[0].number = newList.agent_order_count
				this.loading = false
				this.$util.hideAll()
			},
			handerTabChange(index) {
				this.activeIndex = index;
				this.$util.showLoading()
				this.param.page = 1;
				this.getList()
			},
			// 拒绝接单
			async toRefuse(index) {
				this.index = index
				this.coach_refund_text = ''
				this.$refs.refuse_item.open()
			},
			// 确认：拒绝接单
			async confirmRefuse() {
				let {
					coach_refund_text
				} = this
				coach_refund_text = coach_refund_text.length > 0 ? coach_refund_text.replace(/(^\s*)|(\s*$)/g, "") : ''
				if (coach_refund_text.length == 0) {
					this.$util.showToast({
						title: `请输入拒单原因`
					})
					return
				}
				this.coach_refund_text = this.coach_refund_text.substring(0, 200)
				let {
					index,
				} = this
				this.toConfirm(index, -1)
			},
			// type: -1拒绝接单，3确定接单，4已出发，5已到达，6开始服务，7服务完成
			async toConfirm(index, type) {
				let {
					id: order_id,
				} = this.list.data[index]
				let param = {
					order_id,
					type,
				}
				if (type == -1) {
					param.coach_refund_text = this.coach_refund_text
				}
				if (type == 5 || type == 7) {
					let [res_upload, {
						tempFiles = [],
						tempFilePath = ''
					}] = await uni.chooseImage({
						count: 1,
						sourceType: ['camera']
					})
					let {
						attachment_path
					} = await this.$api.base.uploadFile({
						filePath: tempFiles[0].path,
						filetype: 'picture'
					})
					if (!attachment_path) return
					// #ifdef H5
					if (this.$jweixin.isWechat()) {
						this.$util.showLoading()
						// await this.$jweixin.initJssdk();
						await this.$jweixin.wxReady2();
						let {
							latitude: lat = 0,
							longitude: lng = 0
						} = await this.$jweixin.getWxLocation()
						if (!lat) {
							this.$util.hideAll()
							this.$util.showToast({
								title: `请授权定位当前地址`
							})
							return
						}
						if (lat && lng) {
							let key = `${lat},${lng}`
							let data = await this.$api.base.getMapInfo({
								location: key
							})
							let {
								status,
								result
							} = JSON.parse(data)
							if (status == 0) {
								let {
									address
								} = result
								this.toConfirmUpdate(index, type, param, {
									lat,
									lng,
									address,
									attachment_path
								})
							}
						}
					}
					// #endif
					// #ifndef H5
					let {
						lat = '',
							lng = '',
							address = ''
					} = await this.$util.getBmapLocation()
					if (!lat) {
						this.$util.showToast({
							title: `请授权定位当前地址`
						})
						return
					}
					this.toConfirmUpdate(index, type, param, {
						lat,
						lng,
						address,
						attachment_path
					})
					// #endif
				} else {
					this.toConfirmUpdate(index, type, param)
				}
			},
			async toConfirmUpdate(index, type, param, location = {}) {
				if (type == 5 || type == 7) {
					let {
						lat,
						lng,
						address,
						attachment_path
					} = location
					if (type == 5) {
						param.arrive_img = attachment_path
						param.arr_lat = lat
						param.arr_lng = lng
						param.arr_address = address
					} else {
						param.end_img = attachment_path
						param.end_lat = lat
						param.end_lng = lng
						param.end_address = address
					}
				}
				let msg = {
					'-1': '已拒绝接单',
					3: '接单成功',
					4: '已成功出发',
					5: '已成功到达',
					6: '已开始服务',
					7: '服务已完成'
				}
				if (this.lockTap) return;
				this.lockTap = true;
				this.$util.showLoading()
				try {
					await this.$api.technician.updateOrder(param)
					if (type == -1) {
						this.$refs.refuse_item.close()
					}
					this.$util.showToast({
						title: msg[type]
					})
					if (type == 4 || type == 5) {
						this.list.data[index].pay_type = type
					} else {
						this.list.data.splice(index, 1)
					}
					if (type == -1 || type == 3) {
						this.tabList[0].number = this.tabList[0].number - 1
					}
					this.lockTap = false;
					this.$util.hideAll()
					if (type == 6) {
						let {
							time_long,
							start_service_time
						} = await this.$api.order.orderInfo({
							id: param.order_id
						})
						let start_service_time_unix = this.$util.DateToUnix(start_service_time) +
							time_long * 60
						this.unix_list.push({
							id: param.order_id,
							time_long,
							start_service_time,
							start_service_time_unix
						})
					}
				} catch (e) {
					setTimeout(() => {
						this.lockTap = false
						this.$util.hideAll()
					}, 2000)
					return
				}
			},
			// 咨询
			toTel(index) {
				let {
					mobile: url
				} = this.list.data[index].address_info
				this.$util.goUrl({
					url,
					openType: `call`
				})
			},
			// 订单详情
			goDetail(index) {
				let {
					id
				} = this.list.data[index]
				let url = `/technician/pages/order/detail?id=${id}`
				this.$util.goUrl({
					url
				})
			}
		}
	}
</script>


<style lang="scss">
</style>
